package com.newproject.resmi;

import java.util.LinkedList;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class Portfolio extends ListActivity {
	
	private ProgressDialog progressDialog = null;
	private LinkedList<MyInstrument> myInstruments;
	private MyInstrumentAdapter my_instr_adapter;
	private Runnable viewMyInstruments;
	private volatile Thread getMyInstrumentsThread;
	
	private static final String USER_DETAILS = "UserPrefs";
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.portfolio);
        
        this.myInstruments = new LinkedList<MyInstrument>();        
        this.my_instr_adapter = new MyInstrumentAdapter(this, R.layout.my_instrument_row, myInstruments);
        setListAdapter(this.my_instr_adapter);
        
        viewMyInstruments = new Runnable() {
			// @Override
			public void run() {
				getMyInstruments();			
			}
		};
		/*if (getMyInstrumentsThread == null) {
			getMyInstrumentsThread = new Thread(null, viewMyInstruments,
					"MagentoBackground");
			getMyInstrumentsThread.start();
		}
		progressDialog = ProgressDialog.show(Portfolio.this,
				this.getString(R.string.wait),
				this.getString(R.string.loading), true);*/
    }
    
    @Override
    public void onPause() {
    	super.onPause();
    }
    
    @Override
    public void onResume() {    	
    	super.onResume();
    	getMyInstrumentsThread = new Thread(null, viewMyInstruments, "Get My Instruments Thread");
    	getMyInstrumentsThread.start();
    	progressDialog = ProgressDialog.show(Portfolio.this,
				this.getString(R.string.wait),
				this.getString(R.string.loading), true);
    }
    
    private Runnable returnRes = new Runnable() {
		// @Override
		public void run() {
			my_instr_adapter.notifyDataSetChanged();
			progressDialog.dismiss();
		}
	};
	
    private void getMyInstruments(){
    	SharedPreferences settings = getSharedPreferences(USER_DETAILS, Context.MODE_PRIVATE);;					
		String clientId = settings.getString("clientId", "error");		
    	DataManager.fillMyInstruments(myInstruments, clientId);
    	runOnUiThread(returnRes);
   }
       
    private class MyInstrumentAdapter extends ArrayAdapter<MyInstrument> {
    	private LinkedList<MyInstrument> items;

    	public MyInstrumentAdapter(Context context, int textViewResourceId, LinkedList<MyInstrument> items) {
            super(context, textViewResourceId, items);
            this.items = items;
    	}

    	public View getView(int position, View convertView, ViewGroup parent) {
    		View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); // TODO: read about getSystemService
                v = vi.inflate(R.layout.my_instrument_row, null);
            }
            MyInstrument o = items.get(position);
            if (o != null) {            	
                TextView full_ticker = (TextView) v.findViewById(R.id.full_ticker);
                TextView currency = (TextView) v.findViewById(R.id.currency);
                TextView available_amount = (TextView) v.findViewById(R.id.available);
                TextView blocked_amount = (TextView) v.findViewById(R.id.blocked);
                if (full_ticker != null) {
                	full_ticker.setText(o.getFullTicker());                            
                }
                //Resources res = getResources();
                if(currency != null){
                	currency.setText(o.getCurrencyName( getResources()));                 	
                }
                if(available_amount != null){
                	available_amount.setText(o.format(o.getFreeAmount()));                 	
                }
                if(blocked_amount != null){
                	blocked_amount.setText(o.format(o.getBlockedAmount()));                 	
                }
            }
            return v;
    	}
    }
}
